import { addPostApi, modifyPostApi, getPostListApi, deletePostApi } from '@/api/post'
import { obj2newField } from '@/utils'

class PostService {
  getPostList(queryParam) {
    const params = {
      ...queryParam
    }
    return new Promise((resolve, reject) => {
      getPostListApi(params).then((res) => {
        if (res && res.code === 0) {
          const obj = { ...res }
          obj.data.rows = obj.data.rows.map((item) => {
            return obj2newField(item, {
              post_code: 'code',
              post_name: 'name',
              post_sort: 'sort',
              post_id: 'id'
            })
          })
          resolve(obj)
        } else {
          return reject
        }
      }).catch((err) => {
        return reject(err)
      })
    })
  }

  async addPost(postForm) {
    const params = {
      post_code: postForm.code,
      post_name: postForm.name,
      post_sort: postForm.sort,
      status: postForm.status,
      remark: postForm.remark
    }
    return await addPostApi(params)
  }

  async modifyPost(postForm) {
    const params = {
      post_id: postForm.id,
      post_code: postForm.code,
      post_name: postForm.name,
      post_sort: postForm.sort,
      status: postForm.status,
      remark: postForm.remark
    }
    return await modifyPostApi(params)
  }

  async deletePost(postForm) {
    const params = {
      post_id: postForm.post_id
    }
    return await deletePostApi(params)
  }
}

export default PostService
